package com.nine.pattern.proxy.cglib_proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author nine
 * @version 1.0
 * @description TODO
 * @date 2024-07-07 21:25
 */
public class ProxyFactory implements MethodInterceptor {

	private TrainStation station = new TrainStation();

	public TrainStation getProxyObject() {
		// 创建 Enhancer 对象，类似于 JDK 代理中的 Proxy 类
		Enhancer enhancer = new Enhancer();
		// 设置父类字节码对象，指定父类
		enhancer.setSuperclass(TrainStation.class);
		// 设置回调函数
		enhancer.setCallback(this);
		TrainStation proxyObject = (TrainStation) enhancer.create();
		return proxyObject;
	}

	@Override
	public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
		System.out.println("代售点收取费用。。。");
		Object result = method.invoke(station, objects);
		return result;
	}
}
